package com.aaa.dang.modules.login.service.impl;

import com.aaa.dang.common.systemconst.ISystemConst;
import com.aaa.dang.common.util.ResultModel;
import com.aaa.dang.modules.build.service.IRoomService;
import com.aaa.dang.modules.login.service.IWelcomeService;
import com.aaa.dang.modules.review.service.IReviewService;
import com.aaa.dang.modules.user.service.IStatisticsService;
import com.aaa.dang.modules.user.service.IUserService;
import com.aaa.dang.modules.checkin.service.IPayment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.aaa.dang.common.systemconst.ISystemConst.FAULT_CODE;
import static com.aaa.dang.common.systemconst.ISystemConst.SUCCESS_CODE;

/**
 * 欢迎页业务层接口实现
 *
 * @author Dadazhao
 * @date 2018/12/27
 */
@Service
public class WelcomeServiceImpl implements IWelcomeService {

    /**
     * 注入用户管理业务层
     */
    @Autowired
    private IUserService userService;
    /**
     * 注入租金管理业务层
     */
    @Autowired
    private IPayment paymentServce;
    /**
     * 注入房间管理业务层
     */
    @Autowired
    private IRoomService roomService;
    /**
     * 注入统计业务层
     */
    @Autowired
    private IStatisticsService statisticsService;
    @Autowired
    private IReviewService reviewService;
    /**
     * 注入返回结果
     */
    @Autowired
    private ResultModel model;


    /**
     * 获得数量信息
     *
     * @return 总房间数 入住人数 剩余床位数 报修未处理 审核未处理 住宿已逾期
     */
    @Override
    public ResultModel getCountInfo() {
        try {
            //住户人数
            Integer userCount = userService.getCount();
            //逾期人数
            Integer overdueCount = paymentServce.getOverdueCount();
            //房间总数
            Integer roomCount = roomService.getRoomCount();
            //空床位
            Integer emptyBedCount = roomService.getEmptyBedCount();

            //得到未审核总数和报修待处理总数
            Map<String, Integer> reviewMap = reviewService.getliveApplyUnaudited();
            //未审核总数
            Integer applyCount = reviewMap.get("applyCount");
            //报修待处理总数
            Integer repairCount = reviewMap.get("repairCount");
            //用来存储所有的信息
            Map<String, Object> countMap = new HashMap<>();
            countMap.put("userCount", userCount);
            countMap.put("overdueCount", overdueCount);
            countMap.put("roomCount", roomCount);
            countMap.put("emptyBedCount", emptyBedCount);
            countMap.put("applyCount", applyCount);
            countMap.put("repairCount", repairCount);

            //返回时间
            countMap.put("datetime", new Date());


            model.setCode(SUCCESS_CODE);
            model.setData(countMap);
            model.setMsg("查询成功");
            return model;
        } catch (Exception e) {
            e.printStackTrace();
            model.setCode(FAULT_CODE);
            model.setData("");
            model.setMsg("查询失败");
            return model;
        }

    }

}
